package gajdulewiczr;

import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

import javax.media.opengl.GL;

import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureData;
import com.sun.opengl.util.texture.TextureIO;

public class TextureProvider {
//
//	private String[] cubeNames = new String[] { "images/right.tga",
//			"images/left.tga", "images/top.tga", "images/bottom.tga",
//			"images/front.tga", "images/back.tga" };
	
	private String[] cubeNames = new String[] { "images/skybox/posx.jpg",
			"images/skybox/negx.jpg", "images/skybox/posy.jpg", "images/skybox/negy.jpg",
			"images/skybox/posz.jpg", "images/skybox/negz.jpg" };
	
//	private String[] cubeNames = new String[] { "images/skybox/left.jpg",
//			"images/skybox/right.jpg", "images/skybox/up.jpg", "images/skybox/down.jpg",
//			"images/skybox/front.jpg", "images/skybox/back.jpg" };

	private Hashtable<String, Texture> texs = new Hashtable<String, Texture>();

	public boolean loadTexture(String key, String fnm, boolean lin,
			boolean trilin) {
		String fileName = "images/" + fnm;
		Texture tex = null;
		try {
			tex = TextureIO.newTexture(new File(fileName), true);
			if (lin)
				tex.setTexParameteri(GL.GL_TEXTURE_MIN_FILTER,
						GL.GL_LINEAR_MIPMAP_LINEAR);
			else
				tex.setTexParameteri(GL.GL_TEXTURE_MIN_FILTER,
						GL.GL_LINEAR_MIPMAP_NEAREST);
			if (trilin)
				tex.setTexParameteri(GL.GL_TEXTURE_MAG_FILTER,
						GL.GL_LINEAR);
			else
				tex.setTexParameteri(GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
		} catch (Exception e) {
			System.out.println("Error loading texture " + fileName);
			return false;
		}
		texs.put(key, tex);
		return true;
	}

	public Texture getTexture(String key) {
		return texs.get(key);
	}

	public int loadCubeMap(GL gl) {
		// setup cube map
		gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
		gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_WRAP_S,
				GL.GL_REPEAT);
		gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_WRAP_T,
				GL.GL_REPEAT);
		gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_WRAP_R,
				GL.GL_REPEAT);
		gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_MAG_FILTER,
				GL.GL_LINEAR_MIPMAP_LINEAR);
		gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_MIN_FILTER,
				GL.GL_LINEAR_MIPMAP_LINEAR);
		int face = 0;
		TextureData tex;
		for (int i = 0; i < 6; i++) {
			try {
				tex = TextureIO.newTextureData(new File(cubeNames[i]), true,
						TextureIO.JPG);
				switch (i) {
				case 0:
					face = GL.GL_TEXTURE_CUBE_MAP_POSITIVE_X;
					break;
				case 1:
					face = GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_X;
					break;
				case 2:
					face = GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Y;
					break;
				case 3:
					face = GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y;
					break;
				case 4:
					face = GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Z;
					break;
				case 5:
					face = GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z;
					break;
				}
				gl.glTexImage2D(face, 0, tex.getInternalFormat(), tex.getWidth(), tex
						.getHeight(), 0,tex.getPixelFormat(), tex.getPixelType(), tex
						.getBuffer());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return 0;
	}
}
